/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bigdata.collector.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.bigdata.collector.model.Page;

/**
 *
 * @author hu
 */
public class HttpUtils {

	/**
	 * 
	 * @param url
	 * @param count
	 *            重复次数
	 * @return
	 */
	public static Page fetchHttpResponse(String url, int count) {
		return fetchHttpResponse(url, null, count);
	}

	public static Page fetchHttpResponse(String url) throws Exception {
		return fetchHttpResponse(url, null);
	}

	public static Page fetchHttpResponse(String url,
			ConnectionConfig conconfig, int count) {
		HttpRetry httpretry = new HttpRetry(url, conconfig);
		return httpretry.getResult(count);
	}

	public static Page fetchHttpResponseWithSize(String url,
			ConnectionConfig conconfig, int maxsize) throws Exception {
		URL _URL = new URL(url);
		HttpURLConnection con = (HttpURLConnection) _URL.openConnection();
		con.setDoInput(true);
		con.setDoOutput(true);
		con.setRequestProperty(
				"User-agent",
				"Mozilla/5.0 (Linux; Android 4.2.1; Nexus 7 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19");
		if (conconfig != null) {
			conconfig.config(con);
		}
		InputStream is;
		if (con.getResponseCode() == 403)
			is = con.getErrorStream();
		else
			is = con.getInputStream();
		byte[] buf = new byte[2048];
		int read;
		int sum = 0;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		while ((read = is.read(buf)) != -1) {
			if (maxsize > 0) {
				sum = sum + read;
				if (sum > maxsize) {
					read = maxsize - (sum - read);
					bos.write(buf, 0, read);
					Log.Info("cut", "cut size to " + maxsize);
					break;
				}
			}
			bos.write(buf, 0, read);
		}

		is.close();
		Page page = new Page();
		page.content = bos.toByteArray();
		page.url = url;

		page.headers = con.getHeaderFields();
		return page;

	}

	public static Page fetchHttpResponse(String url, ConnectionConfig conconfig)
			throws Exception {
		return fetchHttpResponseWithSize(url, conconfig, Config.maxsize);
	}

}
